<?php
  /*****************************************************
   * Rapidleech 36B Language Class
   *
   * $Id: lang.class.php - 05apr2010-Idx $
  *****************************************************/

class RxLang
{

	var $path;

	var $language;

	var $settings;
	
	var $say = array();

	/**
	 * Set the path for the language folder.
	 * @param string The path to the language folder.
	 */
	function set_path($path)
	{
	    // strip last "/" if exist
		if (substr($path,-1) == PATH_SPLITTER) $path = substr($path,0, -1);
		$this->path = $path;
	}

	/**
	 * Check if a specific language exists.
	 * @param string The language to check for.
	 * @return boolean True when exists, false when does not exist.
	 */
	function language_exists($language)
	{
		$language = preg_replace("#[^a-z0-9\-_]#i", "", $language);
		if(file_exists($this->path."/".$language.".php"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Set the language for an area.
	 * @param string The language to use.
	 */
	function set_language($language="english")
	{		
		$language = preg_replace("#[^a-z0-9\-_]#i", "", $language);

		// Default language is English.
		if($language == "")
		{
			$language = "english";
		}
		
		// Check if the language exists.
		if(!$this->language_exists($language))
		{
			die("Language $language ($this->path".PATH_SPLITTER."$language) is not installed");
		}
		
		$this->language = $language;
		require $this->path.PATH_SPLITTER.$language.".php";
		$this->settings = $langinfo;
	}

	/**
	 * Load the language variables for a section.
	 * @param string The section name.
	 * @param boolean Is this a datahandler?
	 * @param boolean supress the error if the file doesn't exist?
	 */
	function load($section, $supress_error=false)
	{
		// Assign language variables.

		$lfile = $this->path.PATH_SPLITTER.$this->language.PATH_SPLITTER.$section.".lang.php";
		
		if(file_exists($lfile))
		{
			require_once $lfile;
		}
		elseif(file_exists($this->path.PATH_SPLITTER."english".PATH_SPLITTER.$section.".lang.php"))
		{
			require_once $this->path.PATH_SPLITTER."english".PATH_SPLITTER.$section.".lang.php";
		}
		else
		{
			if($supress_error != true)
			{
				die("$lfile does not exist");
			}
		}
		
		if(is_array($l))
		{
			foreach($l as $key => $val)
			{
				if(empty($this->$key) || $this->$key != $val)
				{
					//$this->$key = $val;
					$this->say[$key] = $val;
				}
			}
		}
	}
	
	function sprintf($string)
	{
		$arg_list = func_get_args();
		$num_args = count($arg_list);
		
		for($i = 1; $i < $num_args; $i++)
		{
			$string = str_replace('{'.$i.'}', $arg_list[$i], $string);
		}
		
		return $string;
	}

	/**
	 * Get the language variables for a section.
	 * @return array The language variables.
	 */
	function get_languages($admin=0)
	{
		$dir = @opendir($this->path);
		while($lang = readdir($dir))
		{
			$ext = do_strtolower(get_extension($lang));
			if($lang != "." && $lang != ".." && $ext == "php")
			{
				$lname = str_replace(".".$ext, "", $lang);
				require $this->path.PATH_SPLITTER.$lang;
				$languages[$lname] = $langinfo['name'];				
			}
		}
		@ksort($languages);
		return $languages;
	}

}
?>